<template>
  <el-dialog title="修改角色信息"  :visible.sync="disVisible" @close="closeForm"
             :close-on-click-modal="true">
    <el-form :model="updateRole" :rules="rules" ref="updateForm" label-width="100px">
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="updateRole.name" clearable></el-input>
      </el-form-item>
      <el-form-item label="角色code" prop="code">
        <el-input v-model="updateRole.code" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">确认</el-button>
        <el-button @click="closeForm">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
export default {
  name: "UpdateRole",
  props: {
    formVisible: Boolean,
    formRole: Object
  },
  data() {
    return {
      disVisible: this.formVisible,
      updateRole: this.formRole,
      rules: {
        name: [{required: true, message: '请输入角色名称', trigger: 'blur'}],
        code: [{required: true, message: '请输入角色code', trigger: 'blur'}]
      },
    }
  },
  methods: {
    closeForm() {
      this.$emit('update:updateFormVisible', false)
    },
    submitForm() {
      this.$refs.updateForm.validate(isValid => {
        if (!isValid) {
          this.$message({
            message: "请正确填写表单",
            type: "error",
            duration: 2000
          })
          return;
        }
        this.$axios.postForm("/role/updateRole", this.updateRole).then(response => {
          let responseData = response.data;
          if (responseData.code==200){
            this.$message({
              message:"修改成功",
              type:"success",
              duration:2000
            })
            this.disVisible=false;
            this.$emit('roleList');

          }else {
            this.$message({
              message:"修改失败",
              type:"error",
              duration:2000
            })
            return;
          }
        })
      })
    }
  },
  watch: {
    formVisible(val) {
      this.disVisible = val
    },
    formRole(val) {
      this.updateRole = val
    }
  }

}
</script>

<style scoped>

</style>